package com.epfresh.supplier.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.epfresh.api.constant.AppSupplyRouterConstant;
import com.epfresh.api.entity.RequestEntitySupply;
import com.epfresh.api.entity.ResponseEntity;
import com.epfresh.api.entity.StatusEntity;
import com.epfresh.api.global.BaseApplication;
import com.epfresh.api.global.BaseActivity;
import com.epfresh.api.http.OnRequestListener;
import com.epfresh.api.utils.InputControl;
import com.epfresh.api.utils.VerificationUtils;
import com.epfresh.supplier.R;
import com.epfresh.api.utils.ToastUtils;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

public class MyFeedBackActivity extends BaseActivity {
    private Button suit;// 提交
    private EditText contentET;
    private LinearLayout hint;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        suit = (Button)findViewById(R.id.suit);
        contentET = (EditText)findViewById(R.id.content);
        hint = (LinearLayout)findViewById(R.id.hint);
        suit.setOnClickListener(this);
        hint.setOnClickListener(this);
        initview();
    }

    /**
     * 设置标题 初始化UI
     */
    public void initview() {
        toolbarTitle.setText("意见反馈");
        ivBack.setVisibility(View.VISIBLE);
    }

    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.baseback:
                // 返回上一页
                finish();
                break;
            case R.id.hint:
                hint.setVisibility(View.INVISIBLE);
                contentET.requestFocus();
                InputMethodManager imm = ( InputMethodManager ) this.getSystemService( Context.INPUT_METHOD_SERVICE );
                imm.showSoftInput(getCurrentFocus(), InputMethodManager.SHOW_FORCED);
                break;
            case R.id.suit:
                if (InputControl.isEmpty(contentET)) {
                    ToastUtils.TextToast(getApplicationContext(), "请输入您的意见建议");
                } else {
                    suitsuggestion();
                }
                break;

        }
    }

    /**
     * 提交反馈的意见建议 意见反馈 'app_id'=>'1', 'content'=>'我在测试', 'add_uid'=>1
     */
    private void suitsuggestion() {
        if(InputControl.isEmpty(contentET)){
            ToastUtils.TextToast(this, "反馈不可为空!");
            return;
        }
        if(contentET.getText().toString().length()<10){
            ToastUtils.TextToast(this, "反馈请输入10字以上!");
            return;
        }
        if(contentET.getText().toString().length()>200){
            ToastUtils.TextToast(this, "反馈请输入200字以内!");
            return;
        }
        String hint = contentET.getText().toString();
        if (!VerificationUtils.isValid(hint)){
            toast(getString(R.string.illegal_input, "反馈"));
            return;
        }
        mHttpUp();
    }

    /**
     * 提交反馈
     */
    private void mHttpUp() {

        RequestEntitySupply<Map<String, String>> data = new RequestEntitySupply<>();
        data.setCmd(AppSupplyRouterConstant.cmd_account_suggest_save);
        data.setClientIdentifierCode(getAppApplication().getClientIdentifierCode());
        Map<String, String> map = new HashMap<>();
        //data.setToken(getUser().getToken());
        map.put("accountId", getUser().getAccountId());
        map.put("content",contentET.getText().toString());
        data.setParameters(map);
        request(data, "suggest", onRequestListenerDelete);
    }

    OnRequestListener<StatusEntity> onRequestListenerDelete = new OnRequestListener<StatusEntity>() {
        @Override
        public StatusEntity jsonToObj(String responseStr) {
            return new Gson().fromJson(responseStr, StatusEntity.class);
        }
        @Override
        public void onFail(int failCode, Object arg, Object tag) {

        }

        @Override
        public void onResponseError(int failCode, Object arg, Object tag) {

        }

        @Override
        public void onStart(Object tag) {

        }

        @Override
        public void onResponse(ResponseEntity<StatusEntity> response, Object tag) {
            StatusEntity responseElement = response.getResponseElement();
            if (responseElement.isSuccess()) {
                getUser().delDeliveryAddressCount();
               toast(BaseApplication.getDoc().getBeenSubmitted());
                finish();
            }
        }
    };
}
